package com.whcoding.rabbitmq.example.common;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZipUtils {
    public static final String DEFAULT_CHARSET = "UTF-8";
    public static final String CHARSET_GB2312 = "GB2312";
    public static final String CHARSET_UTF8 = "UTF-8";
    public static final String CHARSET_GBK = "GBK";
    public static final String CHARSET_GB18030 = "GB18030";

    public GZipUtils() {
    }

    public static String gzip(String primStr) {
        return gzip(primStr, "UTF-8");
    }

    /**
     * 压缩
     * @param primStr
     * @param charset
     * @return
     */
    public static String gzip(String primStr, String charset) {
        if (primStr != null && primStr.length() != 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = null;

            try {
                gzip = new GZIPOutputStream(out);
                gzip.write(primStr.getBytes(charset));
            } catch (IOException var13) {
                var13.printStackTrace();
            } finally {
                if (gzip != null) {
                    try {
                        gzip.close();
                    } catch (IOException var12) {
                        var12.printStackTrace();
                    }
                }

            }

            return (new BASE64Encoder()).encode(out.toByteArray());
        } else {
            return primStr;
        }
    }

    public static String gunzip(String compressedStr) {
        return gunzip(compressedStr, "UTF-8");
    }

    /**
     * 解压
     * @param compressedStr
     * @param charset
     * @return
     */
    public static String gunzip(String compressedStr, String charset) {
        if (compressedStr == null) {
            return null;
        } else {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayInputStream in = null;
            GZIPInputStream ginzip = null;
            byte[] compressed = null;
            String decompressed = null;

            try {
                compressed = (new BASE64Decoder()).decodeBuffer(compressedStr);
                in = new ByteArrayInputStream(compressed);
                ginzip = new GZIPInputStream(in);
                byte[] buffer = new byte[1024];
                boolean var8 = true;

                int offset;
                while((offset = ginzip.read(buffer)) != -1) {
                    out.write(buffer, 0, offset);
                }

                decompressed = out.toString(charset);
            } catch (IOException var25) {
                var25.printStackTrace();
            } finally {
                if (ginzip != null) {
                    try {
                        ginzip.close();
                    } catch (IOException var24) {
                    }
                }

                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException var23) {
                    }
                }

                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException var22) {
                    }
                }

            }

            return decompressed;
        }
    }

    public static byte[] gzip(byte[] primStr) {
        if (primStr != null && primStr.length != 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = null;

            try {
                gzip = new GZIPOutputStream(out);
                gzip.write(primStr);
            } catch (IOException var12) {
                var12.printStackTrace();
            } finally {
                if (gzip != null) {
                    try {
                        gzip.close();
                    } catch (IOException var11) {
                        var11.printStackTrace();
                    }
                }

            }

            return out.toByteArray();
        } else {
            return primStr;
        }
    }

    public static byte[] gunzip(byte[] compressedStr) {
        if (compressedStr == null) {
            return null;
        } else {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayInputStream in = null;
            GZIPInputStream ginzip = null;
            byte[] decompressed = null;

            try {
                in = new ByteArrayInputStream(compressedStr);
                ginzip = new GZIPInputStream(in);
                byte[] buffer = new byte[1024];
                boolean var6 = true;

                int offset;
                while((offset = ginzip.read(buffer)) != -1) {
                    out.write(buffer, 0, offset);
                }

                decompressed = out.toByteArray();
            } catch (IOException var23) {
                var23.printStackTrace();
            } finally {
                if (ginzip != null) {
                    try {
                        ginzip.close();
                    } catch (IOException var22) {
                        var22.printStackTrace();
                    }
                }

                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException var21) {
                        var21.printStackTrace();
                    }
                }

                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException var20) {
                        var20.printStackTrace();
                    }
                }

            }

            return decompressed;
        }
    }
}
